$(function () {

    $("body").on('click','#sendRand1',function () {//第一步发送邮件
        var mobile = $("#email1").val();
        var _this = $(this);
        sendBindingEmail(mobile,4,function () {
            _this.hide();
            _this.next().show();
            _this.next().text("(60s)后重新获取");
            var time = 60;
            var it = setInterval(function () {
                time --;
                if(time <=0 ){
                    window.clearInterval(it);
                    _this.show();
                    _this.next().hide();
                    _this.next().text("(60s)后重新获取");
                }else{
                    _this.next().text("("+time+"s)后重新获取");
                }
            },1000);
        });
    }).on('click','#nextStep',function () {//下一步
        var oldEmail = $("#email1").val();
        var oldEmailCode = $("#emailCode1").val();
        //验证原的邮箱号与验证码
        $.ajax({
            url:baseloaction +'/mobile/uc/binding/verificationOldEmail',
            type:'post',
            dataType:'json',
            data:{
                'email':oldEmail,
                'emailCode':oldEmailCode
            },
            success:function (result) {
                if(result.success==true){
                   $("#step1").addClass("hide");
                   $("#step2").removeClass("hide");
                }else{
                    $.toast(result.message);
                }
            },
            error:function (error) {
                $.toast('系统错误，原邮箱号验证失败');
            }
        });
    }).on('click','#sendRand2',function () {//第二步发送邮件
        var email = $("#email2").val();
        var _this = $(this);
        sendBindingEmail(email,3,function () {
            _this.hide();
            _this.next().show();
            _this.next().text("(60s)后重新获取");
            var time = 60;
            var it = setInterval(function () {
                time --;
                if(time <=0 ){
                    window.clearInterval(it);
                    _this.show();
                    _this.next().hide();
                    _this.next().text("(60s)后重新获取");
                }else{
                    _this.next().text("("+time+"s)后重新获取");
                }
            },1000);
        });
    }).on('click','#endComplete',function () {//确认绑定手机号
        var email = $("#email2").val();
        var emailCode = $("#emailCode2").val();
        var oldEmailCode = $("#emailCode1").val();
        if(oldEmailCode==null || typeof oldEmailCode =='undefined'){
            oldEmailCode = '';
        }
        //执行绑定
        bindingEmail(email,emailCode,oldEmailCode);
    });

    /**
     * 绑定邮箱号
     * @param email 新的邮箱号
     * @param emailCode 新邮箱号验证码
     * @param oldEmailCode 原邮箱号验证码（如果有就传，没有就传null）
     */
    function bindingEmail(email,emailCode,oldEmailCode) {
        $.ajax({
            url:baseloaction +'/mobile/uc/binding/bindingUserEmail',
            type:'post',
            dataType:'json',
            data:{
                'email':email,
                'emailCode':emailCode,
                'oldEmailCode':oldEmailCode
            },
            success:function (result) {
                if(result.success==true){
                    $("input").val("");
                    window.location.href = baseloaction + '/mobile/uc/userInfo';
                }else{
                    $.toast(result.message);
                }
            },
            error:function (error) {
                $.toast('系统错误，邮箱绑定失败');
            }
        });
    }

    /**
     * 发送邮箱验证码
     * @param email 邮箱号
     * @param status 状态，3绑定 ，4更换
     */
    function sendBindingEmail(email,status,callback) {
        $.ajax({
            url:baseloaction +'/mobile/uc/binding/sendBindingEmailCode',
            type:'post',
            data:{
                'email':email,
                'status':status
            },
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback();
                }else{
                    $.toast(result.message);
                }
            },
            error:function (error) {
                $.toast('系统错误，邮箱验证发送失败');
            }
        });
    }
});